package com.example.demo_dongruanD.controller;

import com.example.demo_dongruanD.dto.DocSetDto;
import com.example.demo_dongruanD.dto.SetMealDetailDto;
import com.example.demo_dongruanD.pojo.Setmeal;
import com.example.demo_dongruanD.response.ApiResponse;
import com.example.demo_dongruanD.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sm")
public class SetMealController {

    private final SetMealService setMealService;

    @Autowired
    public SetMealController(SetMealService setMealService) {
        this.setMealService = setMealService;
    }

    /**
     * 用于点击编辑订单的时候返回对应订单中需要编辑的内容，例如检查项，单位、范围
     * */
    @PostMapping("/detail")
    public SetMealDetailDto getSetMealDetail(@RequestBody Map<String, Integer> request) {
        Integer orderId = request.get("orderId");
        return setMealService.getSetMealDetail(orderId);
    }
//  "orderId": 100569001
    /**
     * 根据对应医生所负责的订单的套餐id，传给前端他所负责的的套餐
     * */
    @PostMapping("/list")
    public ApiResponse<List<Setmeal>> listAllSetmeals(@RequestBody DocSetDto docIdDto) {
        ApiResponse<List<Setmeal>> response = new ApiResponse<>();
        List<Integer> smIds = setMealService.findDistinctSmIdsByDocId(docIdDto.getDocId());
        List<Setmeal> setmeals = setMealService.findBySmIds(smIds);

        response.setCode(200);
        response.setMsg("Success");
        response.setData(setmeals);
        response.setTotal(setmeals.size()); // Set the total count of setmeals

        return response;
    }
}
